package com.jr.Controller;

import com.google.gson.Gson;
import com.jr.Pojo.Couriers;
import com.jr.Pojo.Packages;
import com.jr.Service.ICouriersService;
import com.jr.Service.Impl.CouriersServiceImpl;
import com.jr.util.PageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;

@WebServlet("/couriers/*")
public class CouriersController extends HttpServlet {
    ICouriersService ics = new CouriersServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        int index = requestURI.lastIndexOf("/")+1;
        String mName=requestURI.substring(index);
        Class cla = this.getClass();
        Method method=null;
        try{
            method = cla.getMethod(mName, HttpServletRequest.class, HttpServletResponse.class);
            resp.setContentType("application/json;charset=utf-8");
            req.setCharacterEncoding("utf-8");
            method.invoke(this,req,resp);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public void showdt(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PageHelper ph=new PageHelper();
        ph.setDateCount(ics.queryDataCount());
        ph.setPageIndex(Integer.parseInt(req.getParameter("indexpage")));
        ArrayList<Couriers> packages = ics.queryAll(ph);
        ph.setListIndex(packages);
        resp.getWriter().println(new Gson().toJson(ph));
    }

    public void create(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        System.out.println(name+":"+phone);
        System.out.println("name字节：" + Arrays.toString(name.getBytes(StandardCharsets.UTF_8)));
        boolean b=ics.addCouriers(name,phone);
        resp.getWriter().println(new Gson().toJson(b));
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String i = req.getParameter("id");
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String current = req.getParameter("current_packages");
        String completed = req.getParameter("completed_packages");
        int id = Integer.parseInt(i);
        int current_packages = Integer.parseInt(current);
        int completed_packages = Integer.parseInt(completed);
        Couriers couriers = new Couriers();
            couriers.setId(id);
            couriers.setName(name);
            couriers.setPhone(phone);
            couriers.setCurrent_packages(current_packages);
            couriers.setCompleted_packages(completed_packages);
        boolean b = ics.updateCouriers(couriers);
        resp.getWriter().println(new Gson().toJson(b));
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String i = req.getParameter("id");
        int id = Integer.parseInt(i);
        boolean b = ics.delCouriers(id);
        resp.getWriter().println(new Gson().toJson(b));
    }

    public void showdesc(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PageHelper ph=new PageHelper();
        ph.setDateCount(ics.queryDataCount());
        ph.setPageIndex(Integer.parseInt(req.getParameter("indexpage")));
        ArrayList<Couriers> packages = ics.queryAllDESC(ph);
        ph.setListIndex(packages);
        resp.getWriter().println(new Gson().toJson(ph));
    }

    public void showstatus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        ArrayList<Couriers> list= new ArrayList<>();
        list=ics.queryByName(name);
        Integer current_packages = list.get(0).getCurrent_packages();
        Integer completed_packages = list.get(0).getCompleted_packages();
        ArrayList arrayList = new ArrayList<>();
        arrayList.add(current_packages);
        arrayList.add(completed_packages);
        resp.getWriter().println(new Gson().toJson(arrayList));
    }

    public void showpackages(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        String name = req.getParameter("couriersname");
        Integer courier_id = ics.queryByName(name).get(0).getId();
        PageHelper ph=new PageHelper();
        ph.setDateCount(ics.selectAllDataCount(courier_id));
        ph.setPageIndex(Integer.parseInt(req.getParameter("indexpage")));
        ArrayList<Packages> packages = ics.selectAll(courier_id,ph);
        ph.setListIndex(packages);
        resp.getWriter().println(new Gson().toJson(ph));
    }

    public void updatestatus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        Packages packages=new Packages();
        packages.setId(id);
        packages.setStatus(status);
        boolean b = ics.updateCurrent_packages(packages);
        if(b){
            int cid = ics.selectIdById(id);
            int current_packages = ics.selectCountCurrentByStatus(cid);
            int completed_packages = ics.selectCountCompleteByStatus(cid);
            Couriers couriers = new Couriers();
                couriers.setId(cid);
                couriers.setCurrent_packages(current_packages);
                couriers.setCompleted_packages(completed_packages);
            boolean b1 = ics.updateByName(couriers);
            if(b1){
                b=true;
                resp.getWriter().println(new Gson().toJson(b));
            }else{
                resp.getWriter().println(new Gson().toJson(b1));
            }
        }else{
            resp.getWriter().println(new Gson().toJson(b));
        }


    }
}
